home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / spheremap / HACKS / SIXVIEWS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.2 KB  |  292 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1998.  */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* cubeview.c - shows 6 cube face views and eye view */
  9.  
  10. #include <math.h>
  11. #include <stdio.h>
  12. #include <GL/glut.h>
  13.  
  14. GLfloat up[3] = { 0, 1, 0 };
  15. GLfloat eye[3] = { 0, 0, -20 };
  16. GLfloat obj[3] = { 0, 0, 0 };
  17.  
  18. static int W, H, w3, h3;
  19.  
  20. int angle = 0;
  21. int downx, downy;
  22.  
  23. void
  24. reshape(int w, int h)
  25. {
  26.     W = w;
  27.     H = h;
  28.  
  29.     w3 = W/3;
  30.     h3 = H/3;
  31. }
  32.  
  33. void
  34. drawView(int drawCenterObject)
  35. {
  36.     /* right green small cube (+5,0,-8) */
  37.     glPushMatrix();
  38.       glTranslatef(5.0, 0.0, -8.0);
  39.       glRotatef(-45, 1.0, 0.0, 1.0);
  40.       glColor3f(0.0, 1.0, 0.0);
  41.       glutSolidCube(2.0);
  42.     glPopMatrix();
  43.     /* left red cube (-5,0,-8) */
  44.     glPushMatrix();
  45.       glTranslatef(-5.0, 0.0, -8.0);
  46.       glRotatef(45, 1.0, 0.0, 1.0);
  47.       glColor3f(1.0, 0.0, 0.0);
  48.       glutSolidCube(6.0);
  49.     glPopMatrix();
  50.     /* left blue cube (-7,0,0); */
  51.     glPushMatrix();
  52.       glTranslatef(-7.0, 0.0, 0.0);
  53.       glColor3f(0.0, 0.0, 1.0);
  54.       glutSolidCube(5.0);
  55.     glPopMatrix();
  56.     /* right cyan big cube (+7,0,0) */
  57.     glPushMatrix();
  58.       glTranslatef(7.0, 0.0, 0.0);
  59.       glRotatef(30, 1.0, 1.0, 0.0);
  60.       glColor3f(0.0, 1.0, 1.0);
  61.       glutSolidCube(5.0);
  62.     glPopMatrix();
  63.     /* distant yellow sphere (0,0,+10) */
  64.     glPushMatrix();
  65.       glTranslatef(0.0, 0.0, 10.0);
  66.       glColor3f(1.0, 1.0, 0.0);
  67.       glutSolidSphere(7.0, 8, 8);
  68.     glPopMatrix();
  69.     /* top white sphere (0,+5,0) */
  70.     glPushMatrix();
  71.       glTranslatef(0.0, 5.0, 0.0);
  72.       glColor3f(1.0, 1.0, 1.0);
  73.       glutSolidSphere(2.0, 8, 8);
  74.     glPopMatrix();
  75.     /* bottom magenta big sphere (0,-6,0) */
  76.     glPushMatrix();
  77.       glTranslatef(0.0, -6.0, 0.0);
  78.       glColor3f(1.0, 0.0, 1.0);
  79.       glutSolidSphere(4.0, 8, 8);
  80.     glPopMatrix();
  81.  
  82.     if (drawCenterObject) {
  83.         glPushMatrix();
  84.             glScalef(3.0, 3.0, 3.0);
  85.             glColor3f(1.0, 1.0, 1.0);
  86.             glutSolidIcosahedron();
  87.         glPopMatrix();
  88.     }
  89. }
  90.  
  91. void
  92. setViewArea(GLint x, GLint y, GLsizei w, GLsizei h)
  93. {
  94.     glViewport(x, y, w, h);
  95.     glScissor(x, y, w, h);
  96. }
  97.  
  98. void
  99. positionLights(void)
  100. {
  101.     static GLfloat light1Pos[4] = { -41.0, 41.0, -41.0, 1.0 };
  102.     static GLfloat light2Pos[4] = { +41.0, 0.0, -41.0, 1.0 };
  103.     static GLfloat light3Pos[4] = { -41.0, 0.0, +41.0, 1.0 };
  104.     static GLfloat light4Pos[4] = { +41.0, 0.0, +41.0, 1.0 };
  105.  
  106.     glLightfv(GL_LIGHT0, GL_POSITION, light1Pos);
  107.     glLightfv(GL_LIGHT1, GL_POSITION, light2Pos);
  108.     glLightfv(GL_LIGHT2, GL_POSITION, light3Pos);
  109.     glLightfv(GL_LIGHT3, GL_POSITION, light4Pos);
  110. }
  111.  
  112. struct {
  113.     GLfloat angle;
  114.     GLfloat x, y, z;
  115. } faceInfo[6] = {
  116.     {   0.0, +1.0,  0.0,  0.0 },  /* front */
  117.     {  90.0, -1.0,  0.0,  0.0 },  /* top */
  118.     {  90.0, +1.0,  0.0,  0.0 },  /* bottom */
  119.     {  90.0,  0.0, -1.0,  0.0 },  /* left */
  120.     {  90.0,  0.0, +1.0,  0.0 },  /* right */
  121.     { 180.0, -1.0,  0.0,  0.0 }   /* back */
  122. };
  123.  
  124. void
  125. configFace(int i)
  126. {
  127.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  128.     glMatrixMode(GL_MODELVIEW);
  129.     glLoadIdentity();
  130.     glRotatef(faceInfo[i].angle,
  131.         faceInfo[i].x, faceInfo[i].y, faceInfo[i].z);
  132.     gluLookAt(obj[0], obj[1], obj[2],  /* "eye" at object */
  133.               eye[0], eye[1], eye[2],  /* looking at eye */
  134.               up[0], up[1], up[2]);
  135.     positionLights();
  136. }
  137.  
  138. void
  139. display(void)
  140. {
  141.     /* initial clear */
  142.     glViewport(0, 0, W, H);
  143.     glClearColor(0.5, 0.5, 0.5, 0.5);  /* clear to gray */
  144.     glDisable(GL_SCISSOR_TEST);
  145.     glClear(GL_COLOR_BUFFER_BIT);
  146.     glEnable(GL_SCISSOR_TEST);
  147.     glClearColor(0.0, 0.0, 0.0, 0.0);
  148.  
  149.     /* eye view (bottom right) */
  150.     setViewArea(2*w3+4, 4, w3 - 8, h3 - 8);
  151.  
  152.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  153.     glMatrixMode(GL_PROJECTION);
  154.     glLoadIdentity();
  155.     gluPerspective(60.0, 1.0, 0.5, 40.0);
  156.  
  157.     glMatrixMode(GL_MODELVIEW);
  158.     glLoadIdentity();
  159.     gluLookAt(eye[0], eye[1], eye[2],  /* eye at eye */
  160.               obj[0], obj[1], obj[2],  /* looking at object */
  161.               up[0], up[1], up[2]);
  162.  
  163.     positionLights();
  164.     drawView(1);
  165.  
  166.     /* six views from object... */
  167.  
  168.     /* Projection has 90 degree field of view (6 make a cube). */
  169.     glMatrixMode(GL_PROJECTION);
  170.     glLoadIdentity();
  171.     gluPerspective(90.0, 1.0, 0.5, 40.0);
  172.  
  173.     /* front view (center) */
  174.     setViewArea(w3, h3, w3, h3);
  175.     configFace(0);
  176.     drawView(0);
  177.  
  178.     /* top view */
  179.     setViewArea(w3, 2*h3, w3, h3);
  180.     configFace(1);
  181.     drawView(0);
  182.  
  183.     /* bottom view */
  184.     setViewArea(w3, 0, w3, h3);
  185.     configFace(2);
  186.     drawView(0);
  187.  
  188.     /* left view */
  189.     setViewArea(0, h3, w3, h3);
  190.     configFace(3);
  191.     drawView(0);
  192.  
  193.     /* right view */
  194.     setViewArea(2*w3, h3, w3, h3);
  195.     configFace(4);
  196.     drawView(0);
  197.  
  198.     /* back view (top left) */
  199.     setViewArea(4, 2*h3 + 4, w3 - 8, h3 - 8);
  200.     configFace(5);
  201.     drawView(0);
  202.  
  203.     glutSwapBuffers();
  204. }
  205.  
  206. void
  207. menu(int value)
  208. {
  209.     switch (value) {
  210.     case 1:
  211.         glEnable(GL_LIGHTING);
  212.         printf("enable\n");
  213.         break;
  214.     case 2:
  215.         glDisable(GL_LIGHTING);
  216.         printf("disable\n");
  217.         break;
  218.     }
  219.     glutPostRedisplay();
  220. }
  221.  
  222. void
  223. motion(int x, int y)
  224. {
  225.     /* Pretty weak viewing model.  Eye gets located */
  226.     /* on cylinder on the out skirts of the scene. */
  227.     angle += (x - downx);
  228.     angle = angle % 360;
  229.     eye[0] = sin(angle * 3.14159/180.0) * -20;
  230.     eye[2] = cos(angle * 3.14159/180.0) * -20;
  231.     eye[1] = eye[1] + 0.1 * (y - downy);
  232.     if (eye[1] > +15) eye[1] = +15;
  233.     if (eye[1] < -15) eye[1] = -15;
  234.     glutPostRedisplay();
  235.     downx = x;
  236.     downy = y;
  237. }
  238.  
  239. void
  240. mouse(int button, int state, int x, int y)
  241. {
  242.     if (button == GLUT_LEFT_BUTTON) {
  243.         if (state == GLUT_DOWN) {
  244.             downx = x;
  245.             downy = y;
  246.         }
  247.     }
  248. }
  249.  
  250. void
  251. initGraphicsState(void)
  252. {
  253.     GLfloat lightColor[4] = { 0.6, 0.6, 0.6, 1.0 };  /* white */
  254.  
  255.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  256.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
  257.     glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor);
  258.     glLightfv(GL_LIGHT2, GL_DIFFUSE, lightColor);
  259.     glLightfv(GL_LIGHT3, GL_DIFFUSE, lightColor);
  260.     glEnable(GL_LIGHTING);
  261.     glEnable(GL_LIGHT0);
  262.     glEnable(GL_LIGHT1);
  263.     glEnable(GL_LIGHT2);
  264.     glEnable(GL_LIGHT3);
  265.     glEnable(GL_DEPTH_TEST);
  266.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  267.     glEnable(GL_COLOR_MATERIAL);
  268.     glEnable(GL_NORMALIZE);
  269. }
  270.  
  271. int
  272. main(int argc, char **argv)
  273. {
  274.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  275.     glutCreateWindow("sixviews");
  276.     glutDisplayFunc(display);
  277.     glutReshapeFunc(reshape);
  278.  
  279.     initGraphicsState();
  280.  
  281.     glutCreateMenu(menu);
  282.     glutAddMenuEntry("light on", 1);
  283.     glutAddMenuEntry("light off", 2);
  284.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  285.  
  286.     glutMouseFunc(mouse);
  287.     glutMotionFunc(motion);
  288.  
  289.     glutMainLoop();
  290.     return 0;
  291. }
  292.